package com.howie.parnote.util;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.StringWriter;
import java.util.List;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/14 0014.
 * @version v1.0.0
 */
public class JSONUtil {

	private static Logger logger = LogUtil.getLogger(JSONUtil.class);

	public static ObjectMapper mapper = new ObjectMapper();

	public static String getJsonArray(List list) {
		String jsonArray = "[]";
		if (list != null && list.size() > 0) {

			StringWriter writer = new StringWriter();
			try {
				JsonGenerator jg = mapper.getJsonFactory().createJsonGenerator(writer);
				jg.writeObject(list);
				jsonArray = writer.toString();
				writer.flush();
				writer.close();
			} catch (Exception e) {
				logger.error("尝试JSON转换时出错:" + list + ",toString:" + list.toString());
			}
		}
		return jsonArray;
	}


}
